<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <style type="text/css">
        html,body{width: 100%;height: 100%;margin: 0;padding: 0}
        #tree{width: 100%!important;}
        #online{padding-top:0 !important;}
        #online li img{vertical-align:middle;max-width:100%;max-height:106px;position:absolute;top:50%;transform:translate(-50%,-50%);left:50%;z-index:1;}
        .saveBtn{display:inline-block;*display:inline;*zoom:1;margin-left:10px;padding:0 18px;height:40px;line-height:40px;cursor:pointer;text-align:center;background:#fff;border:1px solid #CFCFCF;border-radius:3px;color:#565656;font-size:14px;}
        .tabs li:hover{color:#fff;background:#00B7EE;border-color:transparent;}
        .nonepic{text-align:center;color:#888;margin-top:60px;}
        .heading{clear:both;margin:20px 10px;}
        .heading h2{color:#333333;font-size:15px;margin:0px;margin-bottom:10px;font-weight:normal;}
        .heading hr{border:none;position:relative;margin:0px;height:1px;width:100%;background:#e5e5e5;}
        .heading hr:before{height:2px;background:#666666;content:"";position:absolute;top:-1px;width:60px;left:0px;}
    </style>
    <link href="__ADMIN__/lib/Ueditor/dialogs/image/image.css" rel="stylesheet" type="text/css">
    <link href="__ADMIN__/lib/layui-v2.5.5/css/layui.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="__COMMON__/dtree/dtree.css">
    <link rel="stylesheet" href="__COMMON__/dtree/font/dtreefont.css">
</head>
<body>
<div class="layui-container" style="width: 100%;padding: 0;height: 100%">
    <div class="layui-row">
        <div class="layui-col-md2" style="background: #F9FCFF" id="tree-div">
            <div id="tree" style="margin-top: 50px;"></div>
        </div>
        <div class="layui-col-md10">
            <button type="button" class="layui-btn layui-btn-normal" style="margin-left: 10px;margin-top: 10px" onclick="usePic()">确认使用</button>
            <div class="ui-layout-center" id="online">
                <ul id="list" class="tabs">
                    <div class="heading">
                        <h2>图片内容</h2>
                        <hr>
                    </div>
                    <div id="show-img"></div>
                    <script id="tpl" type="text/html">
                        {{#  layui.each(d.data, function(index, item){ }}
                        <li data-url="{{ item.url }}" onclick='hover_new(this);'>
                            <img src="{{ item.url }}"/>
                            <span class="icon"></span>
                        </li>
                        {{#  }); }}
                        {{#  if(d.data.length === 0){ }}
                        <div class='nonepic'>暂无图片可选择~</div>
                        {{#  } }}
                    </script>
                </ul>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="__COMMON__/jquery.min.js"></script>
<script type="text/javascript" src="__ADMIN__/lib/layui-v2.5.5/layui.js"></script>
<script type="text/javascript">
    // 记录选择的图片
    function hover_new(this_) {
        var num = '{$num}';
        if (1 == num) {
            // 当数量为1时，仅可选择一张图片
            $("#list li").each(function(){
                $(this).removeClass("selected")
            });
        }

        // 给点击的图片加上Class
        $(this_).toggleClass("selected");
    }

    var data = [{$tree|raw}];

    layui.extend({
        dtree: '{/}__COMMON__/dtree/dtree'
    }).use(['dtree','layer', 'laytpl'], function() {
        var dtree = layui.dtree, layer = layui.layer;
        var laytpl = layui.laytpl;

        dtree.render({
            elem: "#tree",
            data: data,
            checkbar: true,
            dataStyle: 'layStyle',
            initLevel: "0",
            done: function(res, $ul, first){
                if(first) {
                    dtree.click('tree', "3");
                }
            }
        });

        dtree.on("node('tree')", function(obj) {
            var treeOjb = obj.param;
            var parentInfo = obj.parentParam;
            if ('comimg' == treeOjb.context) {
                var path = parentInfo.context + '/' + treeOjb.context;
            } else if ('storage' != treeOjb.context) {
                var path = 'storage/' + parentInfo.context + '/' + treeOjb.context;
            } else {
                return;
            }

            $.getJSON('{:url("attachment/show")}', {
                path: path
            }, function (res) {

                var getTpl = tpl.innerHTML
                    ,view = document.getElementById('show-img');
                laytpl(getTpl).render(res, function(html){
                    view.innerHTML = html;
                });
            });
        });
    });

    // 使用选中的图片
    function usePic() {
        var picMap = [];
        $("#show-img").find('li').each(function () {
            if ($(this).hasClass('selected')) {
                picMap.push($(this).attr('data-url'));
            }
        });

        parent.useCheckedPic(picMap);
    }

    $(function () {
        $("#tree-div").css('height', $('.layui-container').height() + 'px');
    })
</script>
</body>
</html>